/*
 *
 * I (David P Shenba) licenses this file to you under the Apache License, 
 * Version 2.0 (the "License"); you may not use this 
 * file except in compliance with the License.  You may 
 * obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.shenba.chart;

import java.util.ArrayList;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.shenba.utils.HTMLBuilder;
import org.shenba.utils.ObjectGrabber;

/**
 * Tag for creating a &lt;table&gt; from an ArrayList of Beans. 
 * The Bean object should be a strict Java Bean. 
 * <p>Only ArrayLists are supported at this moment!!!</p> 
 * <p>Your bean should contain String or primitives alone</p>
 * <b>NOTE</b> - This tag requires a Java2 (JDK 1.2 or
 * later) platform.
 * @author David P Shenba
 * @version 1.0
 */
public class TableTag extends TagSupport {
	
	/**
	 * Name of the form
	 */
	private String name = null;
	/**
	 * Name of the collection
	 */
	private String property = null;
	/**
	 * Type of the bean
	 */
	private String type = null;
	/**
	 * Style class for the table
	 */
	private String styleClass = null;
	/**
	 * Id for the table
	 */
	private String styleId = null;
	
	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
	 */
	public int doStartTag() throws JspException {
		ArrayList list = null;
		try {
			  JspWriter out = pageContext.getOut();
			  
			  /*prints the table tag*/
			  out.print("<table");
			  if(styleClass != null) {
				  out.print(" class = \"" + styleClass + "\"");
			  }
			  if(styleId != null) {
				  out.print(" id = \"" + styleId + "\"");
			  }
			  out.print(">");
			  
			  /*prints the table body*/
			  Object formBean = ObjectGrabber
		    	  .getInstance().getForm(pageContext, name);
		      ArrayList beanList = ObjectGrabber
		    	  .getInstance().getArrayFromForm(formBean, property);
		      out.print(HTMLBuilder.getInstance().formattedTable(beanList, type));
			  
		} catch(Exception e) {
			  throw new JspException("Exception in <chart:table> tag", e);
		}
		return EVAL_BODY_INCLUDE;
	}
	
	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
	 */
	public int doEndTag() throws JspException {
		  /*prints the table end tag*/
		try {
		    JspWriter out = pageContext.getOut();
		    out.print("\n</table>");
		  } catch(Exception e) {
			  throw new JspException("Exception in <chart:table> tag", e);
			}
		  return EVAL_PAGE;
		}

	
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the property
	 */
	public String getProperty() {
		return property;
	}
	/**
	 * @param property the property to set
	 */
	public void setProperty(String property) {
		this.property = property;
	}
	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}
	/**
	 * @return the styleClass
	 */
	public String getStyleClass() {
		return styleClass;
	}
	/**
	 * @param styleClass the styleClass to set
	 */
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}
	/**
	 * @return the styleId
	 */
	public String getStyleId() {
		return styleId;
	}
	/**
	 * @param styleId the styleId to set
	 */
	public void setStyleId(String styleId) {
		this.styleId = styleId;
	}

}
